# -*- coding: utf-8 -*-
import time,sqlite3
 # 把列表转成sql的where in语句中的()部分
def c2sql(x):
    if len(x)==1:
        return str(tuple(x))[:-2]+")"
    else:
        return str(tuple(x))

def test():
    # 检查是否有nd增加或删除
    rps=[[4,3,'1.1.1.1','.1.1','00-11-22-33-44-55',0,1],[7,3,'1.1.1.1','.1.1','00-11-22-33-44-55',0,1]]
    tmp=[]
    [tmp.append(x[0]) for x in rps]
    sql="delete from rrp where neid in "+c2sql(set(tmp))
    print sql
    conn=sqlite3.connect(r'lkt.db3')
    conn.text_factory = str
    for x in rps:
        sql="replace into rrp(neid,ndid,ndip,sSubOid,sRpMac,bRpStatus,bAlive) values(?,?,?,?,?,?,?)"
        conn.execute(sql,x)
    conn.commit()
    conn.close()
    
def a():
    conn=sqlite3.connect(r'res3.db3')
    # conn.text_factory = str
    c=conn.execute("select community from tcomm group by community").fetchall()
    for x in c:
        print x
        conn.execute("replace into tProject(projectid,projectname) values(?,?)",(None,x[0]))
    conn.commit()
    conn.close()
    
def b():
    conn=sqlite3.connect(r'res3.db3')
    # conn.text_factory = str
    p=conn.execute("select * from tproject").fetchall()
    for x in p:
        print x
        conn.execute("update tcomm set project=? where community=?",(x[0],x[1]))
    conn.commit()
    conn.close()
    
b()
